package com.jbinfo.mongoot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jbinfo.mongoot.operation.cms.Cms;
import com.jbinfo.mongoot.operation.cms.CmsRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/tstcms")
@Api("ejiaqin-cms类Controller-API")
public class CmsController {
	
	@Autowired
	private CmsRepository cmsRepository;
	
	/**
	 * 获得所有cms信息
	 * @return
	 */
	@ApiOperation("获得所有的cmsList")
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public List<Cms> getAll() {
		return cmsRepository.findAll();
	}
	
	/**
	 * 增加一条cms信息
	 * @param firstname
	 * @param secondname
	 * @return
	 */
	@ApiOperation("增加一条cms信息")
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public Cms add(
			@RequestParam("username") String username, 
			@RequestParam("title") String title,
			@RequestParam("content") String content,
			@RequestParam("type") String type) {
         Cms cms = new Cms();
         cms.setUsername(username);
         cms.setTitle(title);
         cms.setContent(content);
         cms.setType(type);
         return cmsRepository.save(cms);
    }
	
	/**
	 * 添加通过post请求
	 * @param cms
	 * @return
	 */
	@ApiOperation("通过post请求添加cms信息")
	@RequestMapping(value = "/add2", method = RequestMethod.POST)
	public Cms add(@ModelAttribute Cms cms) {
         return cmsRepository.save(cms);
    }
	
	
	/**
	 * 根据username获取cms
	 * @param username
	 * @return
	 */
	@ApiOperation("根据username获取cms")
	@RequestMapping(value = "/getByUsername", method = RequestMethod.GET)
	public Cms getCmsByUsername(@RequestParam("username") String username) {
	     return cmsRepository.findByUsername(username);
	}
	
	/**
	 * 根据Id获取cms
	 * @param cid
	 * @return
	 */
	@ApiOperation("根据Id获取cms")
	@RequestMapping(value = "/getByCid", method = RequestMethod.GET)
	public Cms getCmsById(@RequestParam("cid") String cid) {
	     return cmsRepository.findByCid(cid);
	}
	
	/**
	 * 根据type获取cmsList
	 * @param type
	 * @return
	 */
	@ApiOperation("根据type获取cmsList")
	@RequestMapping(value = "/getListByType", method = RequestMethod.GET)
	public List<Cms> getListByType(@RequestParam("type") String type) {
	     return cmsRepository.findListByType(type);
	}
	
	/**
	 * 根据id删除Cms
	 * @param cid
	 * @return
	 */
	@ApiOperation("根据id删除Cms")
	@RequestMapping(value = "/delById", method = RequestMethod.GET)
	public boolean delCmsById(@RequestParam("cid") String cid) {
		 cmsRepository.delete(cid);
	     return true;
	}
	
	
	
	
}
